﻿//模块名称:权限设置
//功能描述:分配对应角色权限
//创建人:  
//创建时间:
//修改描述:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
using webnew.Model;
using webnew.BLL;
namespace webnew.Systems
{
    /// <summary>
    /// 权限分配
    /// </summary>
    public partial class S_roleSet : WebPageBase.EditBase
    {
        private List<Model.Sys_NavigationInfo> NavList = null;
        private System.Data.DataTable dt = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            _ListSrc = "S_roleList.aspx"; 
            #region 隐藏母板页面按钮
            //this.Master.FindControl("btnSaveBack").Visible = false;
            this.Master.FindControl("btnSaveStay").Visible = false;
            #endregion
            if (!IsPostBack) SetTitle(true, "角色管理 >> 权限设置");
        }

        /// <summary>
        /// 重写基类显示数据方法
        /// </summary>
        protected override void ShowData()
        {
          
            //获取角色信息
            Model.Sys_RoleInfo model = new webnew.BLL.Sys_Role().GetList(Request.QueryString["id"].ToString());
            ltName.Text = "当前角色：<span style='color:red; margin-left:5px'>" + model.rolename + "</span>";
            //获取角色用户
            webnew.Common.CommonHelper.DataGrdBind(grdView, new BLL.Sys_User().GetUserListByRoleId(Request.QueryString["id"].ToString()));
            //绑定导航操作
            NavList = new BLL.Sys_Navigation().GetList();  //获取存在的所有导航和按钮列表
            dt = new BLL.Sys_RoleNavigation().GetRoleNavigation(_RecordId);//根据角色Id获取角色导航列表
           // TreeView1.SelectAction(
            CreateTree(); //创建导航树
        }
        

        #region  递归绑定树控件
        private void CreateTree()
        {
            foreach (Model.Sys_NavigationInfo model in NavList.Where(p => p.ParentID == Guid.Empty.ToString()))
            {
               
                TreeNode ParentNode = new TreeNode(model.NaviName, model.Id.ToString(), "/App_Themes/Images/Icon/"+model.picsrc+".gif");
                ParentNode.ShowCheckBox = true;
                ParentNode.SelectAction = TreeNodeSelectAction.Expand;
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    if (ParentNode.Value == dr["NavigationID"].ToString())
                    {
                        ParentNode.Checked= true;
                    }
                }
                TreeView1.Nodes.Add(ParentNode);
                GetChildNodes(ParentNode);
            }
        }

        private void GetChildNodes(TreeNode ParentNode)
        {
            foreach (Model.Sys_NavigationInfo model in NavList.Where(p => p.ParentID == ParentNode.Value))
            {
                TreeNode ChildNode = new TreeNode(model.NaviName, model.Id.ToString(), "~/App_Themes/Images/Icon/" + model.picsrc + ".gif");
                ChildNode.ShowCheckBox = true;
                ChildNode.SelectAction = TreeNodeSelectAction.Expand;
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    if (ChildNode.Value == dr["NavigationID"].ToString())
                    {
                        ChildNode.Checked = true;
                    }
                }
                if (NavList.Exists(p => p.ParentID == model.Id)) GetChildNodes(ChildNode);
                ParentNode.ChildNodes.Add(ChildNode);
            }
        }
        #endregion
        #region 递归得到所有选中节点
        private string[] GetAllSelectedNode()
        {
            List<string> lst = new List<string>();
            foreach (TreeNode tn in TreeView1.Nodes)
            {
                if (tn.Checked)
                {
                    lst.Add(tn.Value);
                }
                if (tn.ChildNodes.Count > 0)
                {
                    lst.AddRange(GetAllChildNode(tn));
                }
            }
            return lst.ToArray();
        }

        private string[] GetAllChildNode(TreeNode tn)
        {
            List<string> lst = new List<string>();
            foreach (TreeNode tnchild in tn.ChildNodes)
            {
                if (tnchild.Checked)
                    lst.Add(tnchild.Value);
                if (tnchild.ChildNodes.Count > 0)
                {
                    lst.AddRange(GetAllChildNode(tnchild));
                }
            }
            return lst.ToArray();
        }
        #endregion

        /// <summary>
        /// 重写基类保存数据方法
        /// </summary>
        protected override int SaveData()
        {
            return new BLL.Sys_RoleNavigation().RoleSet(_RecordId, GetAllSelectedNode());
        }

        protected void lbtnView_Click(object sender, EventArgs e)
        {
            grdView.Visible = true;
            lbtnHide.Visible = true;
            lbtnView.Visible = false;
        }

        protected void lbtnHide_Click(object sender, EventArgs e)
        {
            grdView.Visible = false;
            lbtnHide.Visible = false;
            lbtnView.Visible =true ;
        }

    }
}
